package com.newworld.basketballfire.service.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by George on 2019/3/10.
 * Activity
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Activity {
    private String id;
    private String name;
    private int type;
    private BigDecimal dues;
    private int userNum;
    private Date date;
    private String info;
    private List<ActivityRecord> records;
    private long lastUpdateTime;
    private int version;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    public Date getDate() {
        return date;
    }

    public Activity(ActivityParams params) {
        this.id = params.getId();
        this.name = params.getName();
        this.userNum = params.getUserNum();
        this.type = params.getType();
        this.dues = params.getDues();
        this.date = params.getDate();
        this.info = params.getInfo();
        this.records = getActivityRecords(params.getRecords());
    }

    private List<ActivityRecord> getActivityRecords(List<ActivityRecordParams> recordParams) {
        if (CollectionUtils.isEmpty(recordParams)) {
            return new ArrayList<>();
        }
        List<ActivityRecord> records = new ArrayList<>();
        for (ActivityRecordParams params : recordParams) {
            ActivityRecord record = new ActivityRecord(params);
            records.add(record);
        }
        return records;
    }
}
